/**
 * 搜索控件（需要根据自身系统部署更改WEBROOT）
 * @author shiwei
 * @type String
 */

var YOUXISOSO_WEB_ROOT = "http://www.youxisoso.com/";

//document.write("<link type='text/css'  media='all' href='" + 
//		YOUXISOSO_WEB_ROOT + 
//		"wp-content/themes/youxisoso/style.css' rel='stylesheet' />");

document.write("<link type='text/css'  media='all' href='" + 
		YOUXISOSO_WEB_ROOT + 
		"wp-content/themes/youxisoso/searchcomponent/css/search.css' rel='stylesheet' />");

document.write("<link type='text/css' href='" + YOUXISOSO_WEB_ROOT + 
		"wp-content/themes/youxisoso/searchcomponent/css/jquery.autocomplete.css' rel='stylesheet' />");

document.write("<script type='text/javascript' src='" + YOUXISOSO_WEB_ROOT + 
		"wp-content/themes/youxisoso/searchcomponent/jquery.js'></script>");

document.write("<script type='text/javascript' src='" + YOUXISOSO_WEB_ROOT +
		"wp-content/themes/youxisoso/searchcomponent/jquery.autocomplete.js'></script>");

var gameid="" ;
var gemetext = "" ;
var  componentid = "" ;
var  resultcomid = "" ;

var cats=new Array()
cats[0]="" ;
cats[1]="6" ;
cats[2]="7" ;
cats[3]="8" ;

function goSearch(event){
	if(event.keyCode==13) {
		getResult(null,null,0) ;
	}
}

/**
 * 初始化
 * @param webroot
 * @return
 */
function init_search_keyword(){
//   	$("#all").autocomplete(
//   			YOUXISOSO_WEB_ROOT+"?page_id=27282",
//   			{selectFirst: false}http://www.youxisoso.com/?page_id=176565
//   	);
	var cat = $("#cat").attr("hiddenValue");
	$("#all").autocomplete(YOUXISOSO_WEB_ROOT+"?page_id=176565&cat="+cat,
			{my_url:YOUXISOSO_WEB_ROOT+"?page_id=176565&cat="+cat+"&keyword="});
}

function fireLiChange(index){
	
	var prex = "gameli";
	for(var i=0 ; i <4;i++){
		if(prex+i == index){
			gameid = cats[i] ;
		}
	}
	gemetext= $("#"+index+" a:only-child").text() ;
	initComponet(componentid,resultcomid);
}
function getResult(getkeyword,getcat,nowpage,fixdiv){
	if(nowpage == undefined){
		nowpage = 0 ;
	}
	if(getkeyword== null || getkeyword == undefined){
		if($('#all')!=null){
			getkeyword = $('#all').val();
		}else{
			getkeyword = "" ;
		}
	}
	if(getcat== null || getcat == undefined){
		if($('#cat')!=null){
			getcat = $('#cat').val();
		}else{
			getcat = "" ;
		}
	}
	var data = {
		'paged':nowpage,
		'keywords':getkeyword,
		'cat':getcat
	};
	if(fixdiv != undefined && fixdiv !=null && fixdiv!=""){
		data ={
			'paged':nowpage,
			'keywords':getkeyword,
			'cat':getcat,
			'fixdiv':fixdiv
		};
	}
	var url = "http://www.youxisoso.com/?page_id=183012&callback=?";
	$.getJSON(
		url,
		data,
		function(msg) {
			var html = "" ;
			html = msg.html ;
			var htmldiv = msg.fixdiv ;
			if(htmldiv == undefined || htmldiv== null || htmldiv ==""){
				$("#" + resultcomid).empty(); 
				$("#" + resultcomid).append(html) ;
			}else {
				$("#" + fixdiv).empty(); 
				$("#" + fixdiv).append(html) ;
			}
		} 
	);

}
/**
 * 插件入口
 * @param {} cid
 */
function initComponet(cid,resultid) {

	componentid = cid ;
	resultcomid = resultid ;
		if($("#" + cid)!= null){
		var html2 = 
			"<div id='list_bottom'>"+
			"	<div id='soso'>"+
	        "		<ul>";
		if(gameid==cats[0]){
			html2+="    		<li id='gameli0' class='menuOn'><a  title='全部游戏' onclick='fireLiChange(\"gameli0\");' >全部游戏</a></li>";
		}else {
			html2+="    		<li id='gameli0' class='menuNo'><a  title='全部游戏' onclick='fireLiChange(\"gameli0\");' >全部游戏</a></li>";
		}
		
		if(gameid==cats[1]){
		    html2+="   		<li id='gameli1' class='menuOn'><a  title='Flash游戏' onclick='fireLiChange(\"gameli1\");' >Flash小游戏</a></li>";
		} else {
			html2+="   		<li id='gameli1' class='menuNo'><a  title='Flash游戏' onclick='fireLiChange(\"gameli1\");' >Flash小游戏</a></li>";
		}
		
	    if(gameid==cats[2]){    
	    	html2+="    		<li id='gameli2' class='menuOn'><a	title='网页游戏' onclick='fireLiChange(\"gameli2\");'  >网页游戏</a></li>";
	    }else{
	    	html2+="    		<li id='gameli2' class='menuNo'><a	title='网页游戏' onclick='fireLiChange(\"gameli2\");'  >网页游戏</a></li>";
	    }
	    
	    if(gameid==cats[3]){
	    	html2+="    		<li id='gameli3' class='menuOn'><a 	title='搜索手机游戏' onclick='fireLiChange(\"gameli3\");'  >手机游戏</a></li>";
	    }else{
	    	html2+="    		<li id='gameli3' class='menuNo'><a 	title='搜索手机游戏' onclick='fireLiChange(\"gameli3\");'  >手机游戏</a></li>";
	    }
	    html2+="		</ul>"+
	        "	</div>"+
	        "	<div id='sousuo1' class='sousuo'>"+
			//"		<form action='"+YOUXISOSO_WEB_ROOT+"' method='get' name='search' id='searchform'> "+
			" 			<input id='cat' name ='cat' type='hidden' value='"+gemetext+"' hiddenValue='"+gameid+"'>" +
			"			<ul>"+
			"				<li class='soso_li'>"+
	        "        			<input type='text' onkeyup='goSearch(event);' " +
	        "						onfocus='init_search_keyword();' id='all' name='s' class='soso_text ac_input' autocomplete='off'>"+
	        "        		</li> "+
	        "        		<li><input type='button' id='sousuoclick' onclick='getResult(null,null,null);' class='soso_button' value='搜索'></li>"+
	        "        		<li><a href='#'>"+
	        "      				<img alt='随便玩玩' id='list_pindaoplay' " +
	        "						src='http://www.youxisoso.com/wp-content/themes/youxisoso/images/suibianwanwan.gif'></a>"+
	        "      			</li>"+
	        "      		</ul>"+
	        //"      	</form>"+ 
	        "  	</div>"+
	        "</div>" ;
		$("#" + cid).empty(); 
		$("#" + cid).append(html2) ;
	}
}


//var total_found = msg.total_found ;
//var time = msg.time ;
//
//var keyword = msg.keyword ;
//var cat = msg.cat;
//var pageSize = msg.pageSize ;
//var paged = msg.paged ;
//
//var data = msg.data ;
//var html = "<div id='list_center'>";
//html = html+"<span id=\'soso_result\' class=\'list_centtopspan\'>&nbsp;&nbsp;&nbsp;&nbsp;找到"+
//	total_found+"个相关游戏     搜索用时:\""+time+"\"  </span>";
//
//for(var i = 0  ; i<data.length ;i++){
//	var id = data[i].id ;
//	var post_title = data[i].post_title ;
//	var post_content = data[i].post_content;
//	var post_game_url = data[i].post_game_url ;
//	var post_game_image = data[i].post_game_image ;
//	var post_category_name = data[i].post_category_name ;
//	var post_link = data[i].post_link ;
//	var cat_title = data[i].cat_title ;
//	html = html+
//	"<div class=\'list\'>"+
//		"<span class=\'list_img\'>"+
//    		"<a href=\'"+post_link+"\'>"+
//				"<img src=\'"+post_game_image+"\' height=\'79\' width=\'104\'>"+
//			"</a>"+
//    	"</span>"+
//		"<h3 class=\'list_title\'>"+
//    		"<a href=\'"+post_link+"\'>"+post_title+"</a>"+
//    	"</h3>"+
//		"<span class=\'list_cat\'>"+
//			cat_title+
//    		":<samp>"+post_category_name+"</samp>"+
//    	"</span>"+
//		"<p class=\'list_neirong\'>"+
//			post_content+
//        "</p>"+
//		"<span class=\'list_add\'>游戏源地址：<samp>"+post_game_url+"</samp></span>"+	 
//	"</div>";
//}


